package gui.view;


import engine.ExternalSource;
import engine.Service;
import gui.view.source.SourcesPanel;

import java.awt.Color;
import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.Hashtable;

import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JSeparator;
import javax.swing.JTabbedPane;
import javax.swing.SwingUtilities;
import javax.swing.UIManager;

import massDistribution.ClassAttributeMap;
import massDistribution.ClassificationAttribute;
import massDistribution.MeasuredAttribute;
import core.FrameOfDiscernment;

/**
 * @author Elisa Costante
 * 
 */
public class EngineMain extends javax.swing.JFrame {

	public static final int MAIN_WIDTH = 800;
	public static final int MAIN_HEIGHT = 600;
	private static final long serialVersionUID = -2216319740336625174L;

	{
		// Set Look & Feel
		try {
			javax.swing.UIManager.setLookAndFeel(UIManager
					.getSystemLookAndFeelClassName());
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private JMenuItem helpMenuItem;
	private JMenu jMenu5;
	private JMenuItem deleteMenuItem;
	private JSeparator jSeparator1;
	private JMenuItem pasteMenuItem;

	private TrustPanel trustPanel;
	private SourcesPanel sourcesPanel;
	private ServicesPanel servicesPanel;
	private AttributesPanel attributesPanel;
	private ResultsPanel resultPanelThis;
	private SimulationPanel simulationPanel;

	private JTabbedPane tabbedPane;
	private JMenuItem copyMenuItem;
	private JMenuItem cutMenuItem;
	private JMenu jMenu4;
	private JMenuItem exitMenuItem;
	private JSeparator jSeparator2;
	private JMenuItem closeFileMenuItem;
	private JMenuItem saveAsMenuItem;
	private JMenuItem saveMenuItem;
	private JMenuItem openFileMenuItem;
	private JMenuItem newFileMenuItem;
	private JMenu jMenu3;
	private JMenuBar jMenuBar1;

	private ArrayList<Service> monitoredServices;
	private Hashtable<String, MeasuredAttribute> attributeList;
	private ArrayList<ExternalSource> sourceList;
	private ArrayList<ClassificationAttribute> classificationAttributesList = new ArrayList<ClassificationAttribute>();
	private ClassAttributeMap classAttributeMap;
	private FrameOfDiscernment frameOfDiscernment;

	/**
	 * Auto-generated main method to display this JFrame
	 */
	public static void main(String[] args) {
		SwingUtilities.invokeLater(new Runnable() {
			public void run() {
				EngineMain inst = new EngineMain();
				System
						.setProperty("apple.awt.fileDialogForDirectories",
								"true");
				inst.setLocationRelativeTo(null);
				inst.setVisible(true);
				inst.setIconImage((new ImageIcon("./icon/engineicon.png")
						.getImage()));
				inst.setTitle("Trust Engine");
				inst.setBackground(CustomizedColor.BACK_GROUND_COLOR);
			}
		});
	}

	public EngineMain() {
		super();

		initGUI();
	}

	private void initGUI() {
		try {
			this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
			this.setSize(new Dimension(MAIN_WIDTH, MAIN_HEIGHT));

			tabbedPane = new JTabbedPane();
			tabbedPane.setBackground(CustomizedColor.TABBED_BACKGROUND_COLOR);
			getContentPane().add(tabbedPane);

			attributesPanel = new AttributesPanel(this);
			tabbedPane.addTab("Attributes Settings", null, attributesPanel,
					null);

			servicesPanel = new ServicesPanel(this);
			tabbedPane.addTab("Services Settings", null, servicesPanel, null);

			sourcesPanel = new SourcesPanel(this);
			tabbedPane.addTab("Sources Settings", null, sourcesPanel, null);

			trustPanel = new TrustPanel(this);
			tabbedPane.addTab("Trust Configuration", null, trustPanel, null);

			simulationPanel = new SimulationPanel(this);
			tabbedPane.addTab("Simulation", null, simulationPanel, null);

			for (int i = 0; i < tabbedPane.getTabCount(); i++) {
				setTabColor(i);
			}
			setJMenu();

		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	/**
	 * @param i
	 */
	private void setTabColor(int i) {
		tabbedPane.setBackgroundAt(i, CustomizedColor.TAB_COLOR);
		tabbedPane.setForegroundAt(i, CustomizedColor.TAB_FOREGROUND_COLOR);
	}

	/**
	 * 
	 */
	private void setJMenu() {
		{
			jMenuBar1 = new JMenuBar();
			jMenuBar1.setBackground(Color.BLACK);
			setJMenuBar(jMenuBar1);
			{
				jMenu3 = new JMenu();
				jMenu3.setBackground(Color.black);
				jMenu3.setForeground(Color.white);
				jMenuBar1.add(jMenu3);
				jMenu3.setText("File");
				{
					newFileMenuItem = new JMenuItem();
					jMenu3.add(newFileMenuItem);
					newFileMenuItem.setText("New");
				}
				{
					openFileMenuItem = new JMenuItem();
					jMenu3.add(openFileMenuItem);
					openFileMenuItem.setText("Open");
				}
				{
					saveMenuItem = new JMenuItem();
					jMenu3.add(saveMenuItem);
					saveMenuItem.setText("Save");
				}
				{
					saveAsMenuItem = new JMenuItem();
					jMenu3.add(saveAsMenuItem);
					saveAsMenuItem.setText("Save As ...");
				}
				{
					closeFileMenuItem = new JMenuItem();
					jMenu3.add(closeFileMenuItem);
					closeFileMenuItem.setText("Close");
				}
				{
					jSeparator2 = new JSeparator();
					jMenu3.add(jSeparator2);
				}
				{
					exitMenuItem = new JMenuItem();
					jMenu3.add(exitMenuItem);
					exitMenuItem.setText("Exit");
					exitMenuItem.setActionCommand("exit");
				}
			}
			{
				jMenu4 = new JMenu();
				jMenu4.setBackground(Color.black);
				jMenu4.setForeground(Color.white);

				jMenuBar1.add(jMenu4);
				jMenu4.setText("Edit");
				{
					cutMenuItem = new JMenuItem();
					jMenu4.add(cutMenuItem);
					cutMenuItem.setText("Cut");
				}
				{
					copyMenuItem = new JMenuItem();
					jMenu4.add(copyMenuItem);
					copyMenuItem.setText("Copy");
				}
				{
					pasteMenuItem = new JMenuItem();
					jMenu4.add(pasteMenuItem);
					pasteMenuItem.setText("Paste");
				}
				{
					jSeparator1 = new JSeparator();
					jMenu4.add(jSeparator1);
				}
				{
					deleteMenuItem = new JMenuItem();
					jMenu4.add(deleteMenuItem);
					deleteMenuItem.setText("Delete");
				}
			}
			{
				jMenu5 = new JMenu();
				jMenu5.setBackground(Color.black);
				jMenu5.setForeground(Color.white);

				jMenuBar1.add(jMenu5);
				jMenu5.setText("Help");
				{
					helpMenuItem = new JMenuItem();
					jMenu5.add(helpMenuItem);
					helpMenuItem.setText("Help");
				}
			}
		}
	}

	/**
	 * @param trustAttributes
	 */
	public void setAttributeList(
			Hashtable<String, MeasuredAttribute> trustAttributes) {
		this.attributeList = trustAttributes;
		trustPanel.addAttributesToCombo();
	}

	/**
	 * @return the attributeList
	 */
	public Hashtable<String, MeasuredAttribute> getAttributeList() {
		return this.attributeList;
	}

	/**
	 * @return the attributeList
	 */
	public ArrayList<MeasuredAttribute> getAttributeArrayList() {

		if (attributeList != null) {
			ArrayList<MeasuredAttribute> list = new ArrayList<MeasuredAttribute>(
					attributeList.size());
			Enumeration<MeasuredAttribute> en = attributeList.elements();
			while (en.hasMoreElements()) {
				MeasuredAttribute attribute = (MeasuredAttribute) en
						.nextElement();
				list.add(attribute);
			}
			return list;
		} else
			return null;
	}

	/**
	 * @return the monitoredServicesList
	 */
	public ArrayList<Service> getMonitoredServices() {
		return monitoredServices;
	}

	/**
	 * @param monitoredServicesList
	 *            the monitoredServicesList to set
	 */
	public void setMonitoredServices(ArrayList<Service> monitoredServices) {
		this.monitoredServices = monitoredServices;
		simulationPanel.addServicesToCombo();
	}

	public Service getMonitoredServiceFromName(String serviceName) {
		if (this.monitoredServices != null) {
			for (Service service : monitoredServices) {
				if (service.getIdentifierName().equalsIgnoreCase(serviceName))
					return service;
			}
		}
		return null;
	}

	/**
	 * @return the tabbedPane
	 */
	public JTabbedPane getTabbedPane() {
		return this.tabbedPane;
	}

	/**
	 * @return the classificationAttributesList
	 */
	public ArrayList<ClassificationAttribute> getClassificationAttributesList() {
		return this.classificationAttributesList;
	}

	/**
	 * @param classificationAttributesList
	 *            the classificationAttributesList to set
	 */
	public void setClassificationAttributesList(
			ArrayList<ClassificationAttribute> classificationAttributesList) {
		this.classificationAttributesList = classificationAttributesList;
	}

	/**
	 * @return the classAttributeMap
	 */
	public ClassAttributeMap getClassAttributeMap() {
		this.classAttributeMap = new ClassAttributeMap(
				classificationAttributesList);
		return this.classAttributeMap;
	}

	/**
	 * @param classAttributeMap
	 *            the classAttributeMap to set
	 */
	public void setClassAttributeMap(ClassAttributeMap classAttributeMap) {
		this.classAttributeMap = classAttributeMap;
	}

	/**
	 * @return the frameOfDiscernment
	 */
	public FrameOfDiscernment getFrameOfDiscernment() {
		return this.frameOfDiscernment;
	}

	/**
	 * @param frameOfDiscernment
	 *            the frameOfDiscernment to set
	 */
	public void setFrameOfDiscernment(FrameOfDiscernment frameOfDiscernment) {
		this.frameOfDiscernment = frameOfDiscernment;
	}

	public void setResultsPanel(ResultsPanel resultpanel) {
		if (this.resultPanelThis == null) {
			tabbedPane.insertTab("Results", null, resultpanel, null, 5);
			setTabColor(5);

		} else {
			tabbedPane.removeTabAt(5);
			tabbedPane.insertTab("Results", null, resultpanel, null, 5);

		}
		this.resultPanelThis = resultpanel;
	}

	/**
	 * @return the sourceList
	 */
	public ArrayList<ExternalSource> getSourceList() {
		return this.sourceList;
	}

	/**
	 * @param sourceList
	 *            the sourceList to set
	 */
	public void setSourceList(ArrayList<ExternalSource> sourceList) {
		this.sourceList = sourceList;
	}

	/**
	 * Returns the <code>source</code> which has the <code>sourceName</code>
	 * identifier or null if such <code>source</code> does not exuist.
	 * 
	 * @param sourceName
	 * @return the <code>source</code> which has the <code>sourceName</code>
	 *         identifier or null.
	 */
	public ExternalSource getSourceFromName(String sourceName) {
		if (sourceList != null) {
			for (ExternalSource source : sourceList) {
				if (source.getIdentifierName().equalsIgnoreCase(sourceName))
					return source;
			}
		}
		return null;
	}

	/**
	 * Set the panel (and all its components) enabled or disabled.
	 * 
	 * @param panel
	 * 
	 * 
	 */
	public static void setEnabledtPanel(Container panel, boolean enabled) {

		for (Component component : panel.getComponents()) {

			if (component instanceof Container) {
				Container cont = (Container) component;
				cont.setEnabled(enabled);
				setEnabledtPanel(cont, enabled);
			} else
				component.setEnabled(enabled);
		}

	}
}
